package com.wan.data.mysql.factory;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.setting.Setting;
import cn.hutool.setting.dialect.Props;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

public class CustomHikariCpFactory extends DataSourceFactory{

    @Override
    protected DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
        Props connProps = new Props();
        String[] var8 = DSFactory.KEY_CONN_PROPS;

        for (String key : var8) {
            String connValue = poolSetting.getAndRemoveStr(key);
            if (StrUtil.isNotBlank(connValue)) {
                connProps.setProperty(key, connValue);
            }
        }

        Props config = new Props();
        config.putAll(poolSetting);
        config.put("jdbcUrl", jdbcUrl);
        if (null != driver) {
            config.put("driverClassName", driver);
        }

        if (null != user) {
            config.put("username", user);
        }

        if (null != pass) {
            config.put("password", pass);
        }

        HikariConfig hikariConfig = new HikariConfig(config);
        hikariConfig.setDataSourceProperties(connProps);
        return new HikariDataSource(hikariConfig);
    }

}
